from mpython import *
import urequests
import ujson
import network
import time
import ntptime

def connetwifi(ssid,pwd):
    my_wifi = wifi()
    while not (my_wifi.sta.isconnected()):
        my_wifi.connectWiFi(ssid,pwd)
    print(my_wifi.sta.ifconfig())
    

class TinyWebDB(object):
 
    def __init__(self,user,secret):
        self.api = "http://tinywebdb.appinventor.space/api"
        self.user = user
        self.secret = secret
        self.url = self.api+"?user="+self.user+"&secret="+self.secret

    
    def gettime(self):
        return str(time.localtime())
 
    def count(self):
        try: 
            r = urequests.get(self.url+"&action=count")
            if r.status_code == 200:  
                j=ujson.loads(r.text)
                return(j['count'])
            else:
                return "Request Failed."        
        except ValueError:        
            return "Invalid username or password."
        except OSError:        
            return "OS Error"            
            
        
    def get(self,tag):
        try:
            r = urequests.get(self.url+"&action=get&tag="+tag)
            if r.status_code == 200:        
                j=ujson.loads(r.text)
                return(j[tag])
            else:
                return "Request Failed"
        except ValueError:        
            return "Invalid username or password"
        except OSError:        
            return "OS Error"
 
 
    def update(self,tag,value):
        try:
            r = urequests.get(self.url+"&action=update&tag="+tag+"&value="+str(value)) 
            if r.status_code == 200: 
                j=ujson.loads(r.text)
                print(j['status'])
            else:
                print("Request Failed.")
        except ValueError:        
            print("Invalid username or password.")
        except OSError:        
            print("OS Error.") 

        
    def add(self,tag,value):
        try:
            r = urequests.get(self.url+"&action=update&tag="+tag+"&value="+str(value)) 
            if r.status_code == 200: 
                j=ujson.loads(r.text)
                print(j['status'])
            else:
                print("Request Failed.") 
        except ValueError:        
            print("Invalid username or password.")
        except OSError:        
            print("OS Error")         
            
        
    def search(self,startNum,num,tag,showtype):
        try:
            r = urequests.get(self.url+"&action=search&no="+str(startNum)+"&count="+str(num)+"&tag="+tag+"&type="+showtype) 
            if r.status_code == 200:  
                j=ujson.loads(r.text)
                if showtype == "showboth":
                    return(j)
                elif showtype == "showvalue":
                    return(j[tag])
                else:
                    return(j[showtype])
            else:
                return "Request Failed"   
        except ValueError:        
            return "Invalid username or password"
        except OSError:        
            return "OS Error"    
    
    
    def delete(self,tag):
        try:
            r = urequests.get(self.url+"&action=delete&tag="+tag)
            _tag = tag
            if r.status_code == 200:       
                print(_tag + " Deleted Successfully.")                     
            else:
                print("Request Failed.")
        except ValueError:        
            print("Invalid username or password.")
        except OSError:        
            print("OS Error")    
